route.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { ApiPath } from "@/app/constant";
  2. import { NextRequest } from "next/server";
  3. import { handle as openaiHandler } from "../../openai";
  4. import { handle as azureHandler } from "../../azure";
  5. import { handle as googleHandler } from "../../google";
  6. import { handle as anthropicHandler } from "../../anthropic";
  7. import { handle as baiduHandler } from "../../baidu";
  8. import { handle as bytedanceHandler } from "../../bytedance";
  9. import { handle as alibabaHandler } from "../../alibaba";
  10. import { handle as moonshotHandler } from "../../moonshot";
  11. import { handle as stabilityHandler } from "../../stability";
  12. import { handle as iflytekHandler } from "../../iflytek";
  13. import { handle as xaiHandler } from "../../xai";
  14. import { handle as chatglmHandler } from "../../glm";
  15. import { handle as proxyHandler } from "../../proxy";
  16. async function handle(
  17. req: NextRequest,
  18. { params }: { params: { provider: string; path: string[] } },
  19. ) {
  20. const apiPath = `/api/${params.provider}`;
  21. console.log(`[${params.provider} Route] params `, params);
  22. switch (apiPath) {
  23. case ApiPath.Azure:
  24. return azureHandler(req, { params });
  25. case ApiPath.Google:
  26. return googleHandler(req, { params });
  27. case ApiPath.Anthropic:
  28. return anthropicHandler(req, { params });
  29. case ApiPath.Baidu:
  30. return baiduHandler(req, { params });
  31. case ApiPath.ByteDance:
  32. return bytedanceHandler(req, { params });
  33. case ApiPath.Alibaba:
  34. return alibabaHandler(req, { params });
  35. // case ApiPath.Tencent: using "/api/tencent"
  36. case ApiPath.Moonshot:
  37. return moonshotHandler(req, { params });
  38. case ApiPath.Stability:
  39. return stabilityHandler(req, { params });
  40. case ApiPath.Iflytek:
  41. return iflytekHandler(req, { params });
  42. case ApiPath.XAI:
  43. return xaiHandler(req, { params });
  44. case ApiPath.ChatGLM:
  45. return chatglmHandler(req, { params });
  46. case ApiPath.OpenAI:
  47. return openaiHandler(req, { params });
  48. default:
  49. return proxyHandler(req, { params });
  50. }
  51. }
  52. export const GET = handle;
  53. export const POST = handle;
  54. export const runtime = "edge";
  55. export const preferredRegion = [
  56. "arn1",
  57. "bom1",
  58. "cdg1",
  59. "cle1",
  60. "cpt1",
  61. "dub1",
  62. "fra1",
  63. "gru1",
  64. "hnd1",
  65. "iad1",
  66. "icn1",
  67. "kix1",
  68. "lhr1",
  69. "pdx1",
  70. "sfo1",
  71. "sin1",
  72. "syd1",
  73. ];